package com.mdb.open.trade.service.trade;


/*
* Copyright (C), 2017-2019 澳发科技(MDB)
* FileName: TfTradeFileBasicInfoService.java
* Author:   you.yichao
* Date:     2019-10-11
* History:
* <author>          <time>          <version>          <desc>
* you.yichao         2019-10-11          V1.00             代码生成器初始生成代码
* -----------------------------------------------------------
* 作者姓名           修改时间           版本号            描述
*/


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdb.bc.api.data.ListResultEx;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.cache.sequence.util.SequenceGenerator;
import com.mdb.bc.database.mybatisplus.PageUtil;
import com.mdb.bc.database.mybatisplus.SuperService;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.enums.PlatformRetCodeEnum;
import com.mdb.open.common.enums.TfSequenceEnum;
import com.mdb.open.common.enums.trade.status.TradeFileApplyStatusEnum;
import com.mdb.open.common.enums.trade.status.TradeFileStatusEnum;
import com.mdb.open.trade.common.MdbDataSource;
import com.mdb.open.trade.domain.bo.trade.CntCompletedTradeFileBo;
import com.mdb.open.trade.domain.bo.trade.QueryTradeFilePageListBo;
import com.mdb.open.trade.domain.bo.trade.TradeFileTempBasicInfoBo;
import com.mdb.open.trade.domain.ko.trade.TradeFilePageListKo;
import com.mdb.open.trade.domain.po.trade.TradeFileBasicInfo;
import com.mdb.open.trade.mapper.trade.TfTradeFileBasicInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 类描述：
 *  融资档案业务基本资料表 服务类
 *
 * @author you.yichao
 * @date 2019-10-11
 */
@Service
@DS(MdbDataSource.TRADE)
public class TfTradeFileBasicInfoService extends SuperService<TfTradeFileBasicInfoMapper, TradeFileBasicInfo> {

    @Autowired
    SequenceGenerator sequenceGenerator;


    public ResultEx<TradeFileBasicInfo> getInfoByNo(String tradeFileNo) {
        return ResultEx.ok(this.baseMapper.selectOne(new LambdaQueryWrapper<TradeFileBasicInfo>()
                .eq(TradeFileBasicInfo::getTradeFileNo, tradeFileNo)));
    }

    public ResultEx<TradeFileBasicInfo> getInfoByBusinessNo(String businessNo) {
        return ResultEx.ok(this.baseMapper.selectOne(new LambdaQueryWrapper<TradeFileBasicInfo>()
                .eq(TradeFileBasicInfo::getBusinessNo, businessNo)));
    }

    public ResultEx checkBizNoIsExits(String businessNo, String customerCode){

        int count = this.baseMapper.selectCount(new LambdaQueryWrapper<TradeFileBasicInfo>()
                .eq(TradeFileBasicInfo::getCustomerCode, customerCode)
                .eq(TradeFileBasicInfo::getBusinessNo, businessNo));

        if (count > 0) {
            return ResultEx.makeResult(PlatformRetCodeEnum.APPLY_BUSSINESS_EXIST);
        }
        return ResultEx.ok();
    }

    public ResultEx<TradeFileBasicInfo> saveBasicInfo(TradeFileTempBasicInfoBo bo) {

        TradeFileBasicInfo info = new TradeFileBasicInfo();
        info.setTradeFileNo(sequenceGenerator.generator(TfSequenceEnum.TRADE_FILE_NO));
        info.setCustomerCode(bo.getCustomerCode());
        info.setCustomerName(bo.getCustomerName());
        info.setMcustomerCode(bo.getMcustomerCode());
        info.setMerchantNo(bo.getMerchantNo());
        info.setMerchantName(bo.getMerchantName());
        info.setBusinessNo(bo.getBusinessNo());
        info.setBusinessType(bo.getBusinessType());
        info.setImporterCnName(bo.getImporterCnName());
        info.setImporterEnName(bo.getImporterEnName());
        info.setImporterAddress(bo.getImporterAddress());
        info.setCustomsAmount(bo.getCustomsAmount());
        info.setItemsAmount(bo.getItemsAmount());
        info.setContractAmount(bo.getContractAmount());
        info.setContractCcy(bo.getContractCcy());
        info.setCustomsTime(bo.getCustomsTime());
        info.setCustomsCnt(bo.getCustomsCnt());
        info.setStatus(bo.getStatus());
        info.setApplyStatus(bo.getApplyStatus());
        info.setExportVeriStatus(bo.getExportVeriStatus());
        info.setSourceType(bo.getSourceType());

        int count = this.baseMapper.insert(info);
        if (count != 1) {
            return ResultEx.fail();
        }

        return ResultEx.ok(info);
    }

    public ResultEx updateBasicInfo(TradeFileTempBasicInfoBo bo) {

        TradeFileBasicInfo updateBasicInfo = new TradeFileBasicInfo();
        updateBasicInfo.setBusinessType(bo.getBusinessType());
        if (ObjectUtil.isNotEmpty(bo.getBusinessNo())) {
            updateBasicInfo.setBusinessNo(bo.getBusinessNo());
        }
        updateBasicInfo.setImporterCnName(bo.getImporterCnName());
        updateBasicInfo.setImporterEnName(bo.getImporterEnName());
        updateBasicInfo.setImporterAddress(bo.getImporterAddress());
        updateBasicInfo.setCustomsAmount(bo.getCustomsAmount());
        updateBasicInfo.setCustomsCnt(bo.getCustomsCnt());
        updateBasicInfo.setItemsAmount(bo.getItemsAmount());
        updateBasicInfo.setContractAmount(bo.getContractAmount());
        updateBasicInfo.setContractCcy(bo.getContractCcy());
        updateBasicInfo.setCustomsTime(bo.getCustomsTime());
        updateBasicInfo.setStatus(bo.getStatus());
        updateBasicInfo.setExportVeriStatus(bo.getExportVeriStatus());

        LambdaUpdateWrapper<TradeFileBasicInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(TradeFileBasicInfo::getCustomerCode, bo.getCustomerCode());
        updateWrapper.eq(TradeFileBasicInfo::getMerchantNo, bo.getMerchantNo());
        updateWrapper.eq(TradeFileBasicInfo::getTradeFileNo, bo.getTradeFileNo());
        updateWrapper.eq(TradeFileBasicInfo::getStatus, TradeFileStatusEnum.TO_BE_ADDED);

        int count = this.baseMapper.update(updateBasicInfo, updateWrapper);
        if (count != 1) {
            return ResultEx.fail();
        }

        return ResultEx.ok();
    }

    public ResultEx<Integer> cntCompletedTradeFile(String customerCode, String merchantNo) {
        CntCompletedTradeFileBo bo = new CntCompletedTradeFileBo();
        bo.setCustomerCode(customerCode);
        bo.setMerchantNo(merchantNo);
        bo.setStatus(TradeFileStatusEnum.COMPLETED);
        return ResultEx.ok(this.baseMapper.countCompletedTradeFile(bo));
    }

    public ResultEx updateBasicInfoWhenCommitApply(TradeFileTempBasicInfoBo bo, String applyOrderNo) {
        TradeFileBasicInfo updateBasicInfo = new TradeFileBasicInfo();
        updateBasicInfo.setApplyStatus(TradeFileApplyStatusEnum.ALREADY_APPLIED);
        updateBasicInfo.setApplyOrderNo(applyOrderNo);

        LambdaUpdateWrapper<TradeFileBasicInfo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(TradeFileBasicInfo::getCustomerCode, bo.getCustomerCode());
        updateWrapper.eq(TradeFileBasicInfo::getTradeFileNo, bo.getTradeFileNo());
        updateWrapper.eq(TradeFileBasicInfo::getStatus, TradeFileStatusEnum.COMPLETED);
        updateWrapper.eq(TradeFileBasicInfo::getApplyStatus, TradeFileApplyStatusEnum.INIT);

        int count = this.baseMapper.update(updateBasicInfo, updateWrapper);
        if (count != 1) {
            return ResultEx.fail();
        }

        return ResultEx.ok();
    }


    public ListResultEx<TradeFilePageListKo> applyPageQuery(QueryTradeFilePageListBo bo) {

        IPage<TradeFilePageListKo> pageList = this.baseMapper.applyPageQuery(PageUtil.initPage(bo), bo);
        return ListResultEx.ok(pageList.getRecords(), pageList.getTotal(), pageList.getCurrent(), pageList.getSize());
    }

}
